home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Jul / di9807rl / stdform.pas < prev    next >
Pascal/Delphi Source File  |  1998-02-26  |  3KB  |  116 lines

  1. unit StdForm;
  2.  
  3. // Display a table of the 20 standard Windows colors.
  4. // Copyright ⌐ 1998 Tempest Software, Inc.
  5.  
  6. interface
  7.  
  8. uses
  9.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   Grids, Menus;
  11.  
  12. type
  13.   TStandardColorForm = class(TForm)
  14.     Grid: TStringGrid;
  15.     PopupMenu: TPopupMenu;
  16.     EditCopy: TMenuItem;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure GridDrawCell(Sender: TObject; Col, Row: Integer; Rect: TRect;
  19.       State: TGridDrawState);
  20.     procedure EditCopyClick(Sender: TObject);
  21.   private
  22.     procedure DefineColor(Row: Integer; Name: string);
  23.   end;
  24.  
  25. var
  26.   StandardColorForm: TStandardColorForm;
  27.  
  28. implementation
  29.  
  30. uses ClipBrd;
  31.  
  32. {$R *.DFM}
  33.  
  34. // Column indices in the grid.
  35. const
  36.   colColor = 0;
  37.   colName = 1;
  38.   colLiteral = 2;
  39.   colRGB = 3;
  40.  
  41. // Get the definition of a standard Windows color. The Row argument
  42. // is the row number in the grid. The palette index is Row-1.
  43. procedure TStandardColorForm.DefineColor(Row: Integer; Name: string);
  44. var
  45.   Color: TColor;
  46.   Ident: string;
  47. begin
  48.   Color := GetNearestColor(Canvas.Handle, PaletteIndex(Row-1));
  49.   Grid.Cells[colName, Row] := Name;
  50.   if ColorToIdent(Color, Ident) then
  51.     Grid.Cells[colLiteral, Row] := Ident;
  52.   Grid.Cells[colRGB, Row] := Format('$%6.6x', [Color]);
  53. end;
  54.  
  55. procedure TStandardColorForm.FormCreate(Sender: TObject);
  56. begin
  57.   Grid.Cells[colColor,   0] := 'Color';
  58.   Grid.Cells[colName,    0] := 'Name';
  59.   Grid.Cells[colLiteral, 0] := 'Delphi Literal';
  60.   Grid.Cells[colRGB,     0] := 'RGB';
  61.  
  62.   // Set up the names of the standard Windows colors.
  63.   DefineColor(1,  'Black');
  64.   DefineColor(2,  'Maroon');
  65.   DefineColor(3,  'Dark Green');
  66.   DefineColor(4,  'Olive');
  67.   DefineColor(5,  'Navy Blue');
  68.   DefineColor(6,  'Purple');
  69.   DefineColor(7,  'Teal');
  70.   DefineColor(8,  'Light Gray');
  71.   DefineColor(9,  'Light Green');
  72.   DefineColor(10, 'Light Blue');
  73.   DefineColor(11, 'Cream');
  74.   DefineColor(12, 'Medium Gray');
  75.   DefineColor(13, 'Dark Gray');
  76.   DefineColor(14, 'Red');
  77.   DefineColor(15, 'Bright Green');
  78.   DefineColor(16, 'Yellow');
  79.   DefineColor(17, 'Blue');
  80.   DefineColor(18, 'Magenta');
  81.   DefineColor(19, 'Cyan');
  82.   DefineColor(20, 'White');
  83. end;
  84.  
  85. // Draw the left-most column of the grid by filling the cell
  86. // with the actual color.
  87. procedure TStandardColorForm.GridDrawCell(Sender: TObject; Col,
  88.   Row: Integer; Rect: TRect; State: TGridDrawState);
  89. begin
  90.   if (Col = colColor) and (Row > 0) then
  91.   begin
  92.     Grid.Canvas.Brush.Color := PaletteIndex(Row-1);
  93.     Grid.Canvas.FillRect(Rect);
  94.   end;
  95. end;
  96.  
  97. // Copy the text of the table to the clipboard, separating
  98. // columns with tabs, and rows with CR-LF line endings.
  99. procedure TStandardColorForm.EditCopyClick(Sender: TObject);
  100. const
  101.   TAB = ^I;
  102.   RETURN = ^M^J;
  103. var
  104.   Text: string;
  105.   Row: Integer;
  106. begin
  107.   Text := '';
  108.   for Row := 1 to 21 do
  109.     Text := Text + Grid.Cells[colName,Row] + TAB +
  110.                    Grid.Cells[colLiteral,Row] + TAB +
  111.                    Grid.Cells[colRGB,Row] + RETURN;
  112.   Clipboard.AsText := Text;
  113. end;
  114.  
  115. end.
  116.